﻿//using ECommon.IO;
//using ENode.Commanding;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading;
//using System.Threading.Tasks;
//using System.Web;

//namespace Web.App_Code
//{
//    public class BaseController : System.Web.Mvc.Controller
//    {
//        private readonly ICommandService _commandService;
//        public BaseController(ICommandService commandService)
//        {
//            _commandService = commandService;
//        }

//        /// <summary>异步发送给定的命令
//        /// </summary>
//        /// <param name="command"></param>
//        /// <param name="millisecondsDelay"></param>
//        /// <returns></returns>
//        private Task<AsyncTaskResult> SendCommandAsync(ICommand command, int millisecondsDelay = 5000)
//        {
//            return _commandService.SendAsync(command).TimeoutAfter(millisecondsDelay);
//        }

//    }


//    public static class Extensions
//    {
//        public static async Task TimeoutAfter(this Task task, int millisecondsDelay)
//        {
//            var timeoutCancellationTokenSource = new CancellationTokenSource();
//            var completedTask = await Task.WhenAny(task, Task.Delay(millisecondsDelay, timeoutCancellationTokenSource.Token));
//            if (completedTask == task)
//            {
//                timeoutCancellationTokenSource.Cancel();
//            }
//            else
//            {
//                throw new TimeoutException("The operation has timed out.");
//            }
//        }
//        public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, int millisecondsDelay)
//        {
//            var timeoutCancellationTokenSource = new CancellationTokenSource();
//            var completedTask = await Task.WhenAny(task, Task.Delay(millisecondsDelay, timeoutCancellationTokenSource.Token));
//            if (completedTask == task)
//            {
//                timeoutCancellationTokenSource.Cancel();
//                return task.Result;
//            }
//            else
//            {
//                throw new TimeoutException("The operation has timed out.");
//            }
//        }
//    }
//}